﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace aPCSuite
{
    public enum SMS_TYPE
    {
        SEND,
        RECEIVE,
    }

    public enum SMS_STATUS
    {
        NEW,
        DELETE,
        READ,
        UNREAD
    }

    public class SMSGroup : IComparable<SMSGroup>
    {
        public string status="";
        public int thread_id;
        public string address = "";
        public string max_date = "";
        public List<sms> sms_list = new List<sms>();
        public Dictionary<int, int> id_list = new Dictionary<int, int>();
        public void Add(sms _sms)
        {
            sms_list.Add(_sms);
        }/**/
        /*public Dictionary<string, sms> sms_list = new Dictionary<string, sms>();
        public void Add(string key, sms _sms)
        {
            sms_list.Add(key, _sms);
        }/**/

        public void AddBinary(sms _sms)
        {
            int indexResult = sms_list.BinarySearch(_sms);
            if (indexResult < 0)
            {
                sms_list.Insert(~indexResult, _sms);
                id_list.Add(_sms.id, ~indexResult);
            }
            else
            {
                sms_list.Insert(indexResult, _sms);
                id_list.Add(_sms.id, indexResult);
            }
        }

        public void RemoveSMSById(int id)
        {
            if (id_list.ContainsKey(id))
            {
                int index = id_list[id];
                if (index >= 0)
                {
                    sms_list.RemoveAt(index);
                    id_list.Remove(id);
                }
            }
        }

        public void RemoveSMS(sms _sms)
        {
            if (id_list.ContainsKey(_sms.id))
            {
                id_list.Remove(_sms.id);
                sms_list.Remove(_sms);
            }
        }

        public int RemoveSMSAt(int index)
        {
            int id = sms_list[index].id;
            if (id_list.ContainsKey(id))
            {
                id_list.Remove(id);
                sms_list.RemoveAt(index);
            }
            return id;
        }

        public int Count()
        {
            return sms_list.Count;
        }

        #region IComparable<SMSGroup> Members
        public int CompareTo(SMSGroup other)
        {
            //return this.create_date.CompareTo(other.create_date);
            return other.max_date.CompareTo(this.max_date);
        }
        #endregion
    }

    public class SMSItems
    {
        /*public Dictionary<string, SMSGroup> group_list = new Dictionary<string, SMSGroup>();

        public SMSGroup GetGroup(string key)
        {
            if (group_list.Count > 0)
            {
                if (group_list.ContainsKey(key))
                {
                    return group_list[key];
                }
            }
            return null;
        }

        public void Add(string key, SMSGroup value)
        {
            group_list.Add(key, value);
        }/**/

        // sort by date
        public List<SMSGroup> group_list = new List<SMSGroup>();
        public Dictionary<int, int> id_list = new Dictionary<int, int>();

        public SMSGroup GetGroup(int index)
        {
            return group_list[index];
        }

        public SMSGroup GetGroupById(int key)
        {
            if (id_list.ContainsKey(key))
            {
                int index = id_list[key];
                if (index >= 0)
                    return group_list[index];
            }
            return null;
        }

        public void AddBinary(SMSGroup value)
        {
            int indexResult = group_list.BinarySearch(value);
            if (indexResult < 0)
            {
                group_list.Insert(~indexResult, value);
                id_list.Add(value.thread_id, ~indexResult);
            }
            else
            {
                group_list.Insert(indexResult, value);
                id_list.Add(value.thread_id, indexResult);
            }
        }/**/

        public void RemoveGroupById(int key)
        {
            if (id_list.ContainsKey(key))
            {
                int index = id_list[key];
                if (index >= 0)
                {
                    group_list.RemoveAt(index);
                    id_list.Remove(key);
                }
            }
        }

        public void RemoveGroup(SMSGroup group)
        {
            if (id_list.ContainsKey(group.thread_id))
            {
                id_list.Remove(group.thread_id);
                group_list.Remove(group);
            }
        }

        public void RemoveGroupAt(int index)
        {
            int key = group_list[index].thread_id;
            if (id_list.ContainsKey(key))
            {
                id_list.Remove(key);
                group_list.RemoveAt(index);
            }
        }
    }

    public class sms : IComparable<sms>
    {
        [XmlElement("id")]
        public int id;
        [XmlElement("from")]
        public string from;
        [XmlElement("from_name")]
        public string from_name;
        [XmlElement("to")]
        public string to;
        [XmlElement("to_name")]
        public string to_name;
        [XmlElement("type")]
        public int type;
        [XmlElement("status")]
        public string status;
        [XmlElement("content")]
        public string content;
        [XmlElement("thread_id")]
        public int thread_id;
        [XmlElement("create_date")]
        public string create_date;

#region IComparable<sms> Members
        public int CompareTo(sms other)
        {
            //return this.create_date.CompareTo(other.create_date);
            return other.create_date.CompareTo(this.create_date);
        }
#endregion
    }

    public class contact
    {
        [XmlElement("id")]
        public int id;
        [XmlElement("number")]
        public string number;
        [XmlElement("name")]
        public string name;
    }

    public class sms_list
    {
        [XmlElement("dev_code")]
        public string dev_code;
        [XmlElement("limit")]
        public int limit;
        [XmlElement("id_list")]
        public string id_list;
        [XmlElement("sms")]
        public sms[] arr_sms;
    }

    public class contact_list
    {
        [XmlElement("dev_code")]
        public string dev_code;
        [XmlElement("count")]
        public int count;
        [XmlElement("contact")]
        public contact[] arr_contact;
    }

    public class sms_del
    {
        [XmlElement("dev_code")]
        public string dev_code;
    }

    public class sms_update
    {
        [XmlElement("dev_code")]
        public string dev_code;
    }

    public class sms_active
    {
        [XmlElement("dev_code")]
        public string dev_code;
    }

    [XmlRoot("protocol")]
    public class protocol : EventArgs
    {
        [XmlElement("code")]
        public int code;
        [XmlElement("trans_id")]
        public string trans_id;
        [XmlElement("sms_list")]
        public sms_list _sms_list;
        [XmlElement("sms_contact")]
        public contact_list _contact_list;
        [XmlElement("sms_del")]
        public sms_del _sms_del;
        [XmlElement("sms_update")]
        public sms_update _sms_update;
        [XmlElement("session_id")]
        public string session_id;
        [XmlElement("sms_active")]
        public sms_active _sms_active;
    }
}
